﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ApqAlarm
{
	/// <summary>
	/// 通过 商通 发短信
	/// </summary>
	public class i2b
	{
		protected static string _uid
		{
			get { return GlobalObject.XmlConfigChain[typeof(i2b), "uid"]; }
		}
		protected static string _pwd
		{
			get
			{
				return GlobalObject.XmlConfigChain[typeof(i2b), "pwd"];
			}
		}
		public static string Url
		{
			get { return GlobalObject.XmlConfigChain[typeof(i2b), "Url"]; }
		}

		public static bool Send(string Mobiles, string Content)
		{
			bool rtn = false;
			StringBuilder sbTemp = new StringBuilder();
			sbTemp.Append("enterpriseid=");
			sbTemp.Append(_uid);
			sbTemp.Append("&pswd=");
			sbTemp.Append(_pwd);
			sbTemp.Append("&mobs=");
			sbTemp.Append(Mobiles);
			sbTemp.Append("&msg=");
			sbTemp.Append(Content);
			sbTemp.Append("&comid=101");

			byte[] bData = Encoding.UTF8.GetBytes(sbTemp.ToString());

			System.Net.HttpWebRequest hwRequest;
			System.Net.HttpWebResponse hwResponse;

			string strResult = string.Empty;
			try
			{
				hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url);
				hwRequest.Timeout = 5000;
				hwRequest.Method = "POST";
				hwRequest.ContentType = "application/x-www-form-urlencoded";
				hwRequest.ContentLength = bData.Length;

				System.IO.Stream smWrite = hwRequest.GetRequestStream();
				smWrite.Write(bData, 0, bData.Length);
				smWrite.Close();

				hwResponse = (HttpWebResponse)hwRequest.GetResponse();
				StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
				strResult = srReader.ReadToEnd();
				srReader.Close();
				hwResponse.Close();

				rtn = (strResult == "100");
			}
			catch (Exception ex)
			{
				Apq.GlobalObject.ApqLog.Error("发送短信失败", ex);
			}
			return rtn;
		}
	}
}
